package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.R;
import com.example.domain.Employee;
import com.example.service.EmployeeService;
import com.example.mapper.EmployeeMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * @author Administrator
 * @description 针对表【employee(员工信息)】的数据库操作Service实现
 * @createDate 2022-04-28 09:38:13
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee>
        implements EmployeeService {

    @Override
    public R login(Employee employee) {
        //1. 获取数据库中此员工
        Employee emp = this.getOne(new LambdaQueryWrapper<Employee>().eq(Employee::getUsername, employee.getUsername()));
        //判断是否存在
        if (emp == null) {
            return R.error("无此账号信息，请注册");
        }
        //判断密码是否正确
        //将页面提交的密码进行MD5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!emp.getPassword().equals(password)) {
            return R.error("密码或者用户名错误");
        }
        //判断是否被禁用
        if (emp.getStatus() == 0) {
            return R.error("此用户被禁用");
        } else {
            return R.success(emp);
        }
    }

    @Override
    public boolean addEmployee(Employee employee) {
        boolean save = this.save(employee);
        return save;
    }

    @Override
    public IPage<Employee> getEmployeeListForPage(Integer page, Integer pageSize, String name) {
        Page<Employee> objectPage = new Page<>(page, pageSize);
        IPage<Employee> page1 = this.page(objectPage, new LambdaQueryWrapper<Employee>().like(Employee::getName, name)
                .orderByDesc(Employee::getUpdateTime));
        return page1;
    }

    @Override
    public boolean updateEmployee(Employee employee) {
//        boolean update = this.update(new LambdaUpdateWrapper<Employee>().eq(Employee::getId, employee.getId())
//                .set(Employee::getStatus, employee.getStatus())
//                .set(Employee::getUpdateTime, employee.getUpdateTime())
//                .set(Employee::getUpdateUser, employee.getUpdateUser()));
        boolean update=this.updateById(employee);
        return update;
    }


}




